package com.h3c.ptability.controller;

import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.h3c.ptability.config.RfcManager;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.SapService;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/ExCommon")
public class SapController {

    @Autowired
    SapService sapservice;

    @Autowired
    CommService commService;

    @Resource
    private RfcManager rfcManager;

    /**
     * 获取sap数据项
     * @return
     */
    @RequestMapping(value = "/getSapDataItems", method = RequestMethod.GET)
    @ResponseBody
    public Result getSapDataItems(){
//        commService.checkRunModeIsProd();
        Object sapDataItems = sapservice.getSapDataItems();
        return new Result(true, 2000, "获取数据成功", sapDataItems);
    }

    /**
     * 同步sap数据项
     * @return
     */
    @RequestMapping(value = "/syncSapData", method = RequestMethod.POST)
    public Result syncSapData(){
        sapservice.saveSap();
        return new Result(true, StatusCodeEnum.NORMAL);
    }

    @RequestMapping("/callSapTest")
    @ResponseBody
    public Result callSapTest(HttpServletRequest request) {
        List list = new ArrayList();
        try {
            JCoFunction function = null;
            JCoDestination destination = null;
            rfcManager.connect();//获取SAP客户端
            destination = rfcManager.getDestination();
            function = rfcManager.getFunction(destination, "ZFM_EHR_GET_ZHRTXZKD");//调用SAP接口
            JCoParameterList tableParameterList = function.getTableParameterList();//获取表格参数
//            JCoTable inputTable = tableParameterList.getTable("T_INPUT");  //获取输入表T_INPUT的数据参数
//            for(int i=0;i<projectCodeList.size();i++){
//                //往输入表中，给指定字段参数进行赋值
//                inputTable.appendRow();
//                inputTable.setRow(i);
//                inputTable.setValue("PROJT", projectCodeList.get(i));
//            }
            function.execute(destination);
            JCoTable outputTable = tableParameterList.getTable("ET_DATA");//获取输出表T_OUTPUT的数据
            for (int i = 0; i < outputTable.getNumRows(); i++) {
                outputTable.setRow(i);
                list.add(outputTable.getString("ZHRBL"));
            }
        } catch (Exception e) {

            e.printStackTrace();
        }


//        JCoFunction function = RfcManager2.getFunction("ZFM_EHR_GET_ZHRTXZKD");
//
//        RfcManager2.execute(function);
//        JCoParameterList outputParam = function.getTableParameterList();
//        JCoTable bt = outputParam.getTable("ET_DATA");
//
//        for (int i = 0; i < bt.getNumRows(); i++) {
//            bt.setRow(i);
//            System.out.println(bt.getString("ZHRBL"));
//        }
        return new Result(true, 20000, " callSapTest 成功", list);
    }

}
